package com.engine.salary.timer;

import com.engine.common.util.ServiceUtil;
import com.engine.salary.entity.datacollection.param.AttendQuoteCheckOperationParam;
import com.engine.salary.entity.datacollection.param.AttendQuoteDataSyncParam;
import com.engine.salary.entity.salarysob.po.SalarySobPO;
import com.engine.salary.service.SalarySobService;
import com.engine.salary.service.impl.SalarySobServiceImpl;
import com.engine.salary.util.SalaryDateUtil;
import com.engine.salary.wrapper.AttendQuoteDataWrapper;
import com.engine.salary.wrapper.AttendQuoteWrapper;
import org.apache.commons.lang3.math.NumberUtils;
import weaver.general.BaseBean;
import weaver.hrm.User;
import weaver.interfaces.schedule.BaseCronJob;

import java.time.LocalDate;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @author Harryxzy
 * @ClassName AutoSyncAttendQuoteData
 * @date 2023/08/11 9:53
 * @description 自动同步所有薪资账套的考勤引用
 */
public class AutoSyncAttendQuoteJob extends BaseCronJob {

    private String preMonth;

    private SalarySobService getSalarySobService(User user) {
        return ServiceUtil.getService(SalarySobServiceImpl.class, user);
    }

    private AttendQuoteWrapper getAttendQuoteWrapper(User user) {
        return ServiceUtil.getService(AttendQuoteWrapper.class, user);
    }
    private AttendQuoteDataWrapper getAttendQuoteDataWrapper(User user) {
        return ServiceUtil.getService(AttendQuoteDataWrapper.class, user);
    }

    @Override
    public void execute() {
        User tempUser = new User();
        tempUser.setUid(1);
        tempUser.setLoginid("sysadmin");
        // 获取所有的薪资账套
        List<SalarySobPO> salarySobList = getSalarySobService(tempUser).listAll();
        LocalDate localDate = SalaryDateUtil.dateToLocalDate(new Date());

        int preMonthValue = -1;
        if (preMonth != null && NumberUtils.isCreatable(preMonth)) {
            preMonthValue = -1 * Integer.valueOf(preMonth);
        }
        localDate = localDate.plusMonths(preMonthValue);

        String dateStr = SalaryDateUtil.getFormatYearMonth(localDate);
        BaseBean baseBean = new BaseBean();
        // 循环所有的账套
        salarySobList.stream().forEach(sob -> {
            // 校验是否可以操作
            Boolean canSync = getAttendQuoteWrapper(tempUser).checkOperation(AttendQuoteCheckOperationParam.builder()
                    .salarySobIds(Collections.singletonList(sob.getId()))
                    .salaryYearMonthStr(dateStr)
                    .build());
            if (canSync) {
                // 同步数据
                try {
                    baseBean.writeLog("自动同步考勤数据开始："+ sob.getId()+","+dateStr);
                    getAttendQuoteDataWrapper(tempUser).syncAttendQuoteData(AttendQuoteDataSyncParam.builder()
                            .salarySobIds(Collections.singletonList(sob.getId()))
                            .salaryYearMonth(dateStr)
                            .build());
                    baseBean.writeLog("自动同步考勤数据结束："+ sob.getId()+","+dateStr);
                } catch (Exception e) {
                    baseBean.writeLog("自动同步考勤数据出错："+ e);
                }
            }
        });
    }
}
